
package com.starshineworks.util;

import com.starshineworks.entities.EntityE;
import com.starshineworks.entities.FieldE;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MenuManager 
{
    private static boolean m_init = false;
    
    private static List<MenuTraits> mtFieldTime = new ArrayList<MenuTraits>();
    private static List<MenuTraits> mtDay       = new ArrayList<MenuTraits>();
        
    List<MenuTraits>     traitList;
    Integer              rid;
    Map<String,String[]> map;

    private static synchronized void initMenuManager(String which) 
    {
        if (!m_init) {
            m_init = true;
            // This by-name hack is needed because Ajax gets only the 'which' string.
            // i.e we serve only a limited set of well-known (registered, you
            // could say) managers. Ideally I could keep a table to make
            // it easier to register managers.
            mtFieldTime.add(new MenuTraits("_field", "field", "drop", 5));
            mtFieldTime.add(new MenuTraits("_time" , "time" , "drop", 5));
            mtDay.add(new MenuTraits("_day", "day", "check", 5));
            mtDay.add(new MenuTraits("_day", "day", "check", 5));
        }
    }
        
    public static MenuManager getMenuManager(String type, Integer rid, Map<String,String[]> m) 
    {
        initMenuManager(type);
        if (type.equalsIgnoreCase("fieldtime")) {
            return new MenuManager(mtFieldTime, rid, m);
        }
        if (type.equalsIgnoreCase("day")) {
            return new MenuManager(mtDay, rid, m);
        }
        return null;

    }
    
    public MenuManager(List<MenuTraits> mt, Integer r, Map<String,String[]> m) 
    {        
        rid       = r;
        traitList = mt;
        map       = m;
    }
            
    public Menu createRequestMenu(Integer menuid) 
    {
        if (menuid > traitList.size() || menuid < 1) {
            return null;
        }
        Menu menu = new Menu(this, menuid);
        return menu;
    }
    
    public List<String[]> getAvailableObjects(String type, Integer mid) 
    {
        if (type.equalsIgnoreCase("field")) {
            return getAvailableFields(mid);
        }
        if (type.equalsIgnoreCase("time")) {
            return getAvailableTimes(mid);
        }
        if (type.equalsIgnoreCase("day")) {
            return getAvailableDays(mid);
        }
        return null;
    }
    
    public List<String[]> getAvailableDays(Integer mid) 
    {
        final ArrayList<String[]> t = new ArrayList<String[]>();
        String[] var = {"0", "0",         ""};
        String[] mon = {"1", "Monday",    "Mon"};
        String[] tue = {"2", "Tuesday",   "Tue"};
        String[] wed = {"3", "Wednesday", "Wed"};
        String[] thu = {"4", "Thursday",  "Thu"};
        String[] fri = {"5", "Friday",    "Fri"};
        t.add(var);
        t.add(mon);
        t.add(tue);
        t.add(wed);
        t.add(thu);
        t.add(fri);
        return t;
    }

    public List<String[]> getAvailableFields(Integer mid) 
    {
        List<String[]> list = new ArrayList<String[]>();
        List<EntityE> ff = EntityE.retrieve(new FieldE());
        Integer i = 1;
        for (EntityE e : ff) {
            FieldE f = (FieldE)e;
            String[] triple = {i.toString(),f._name,f._name};
            list.add(triple);
            i++;
        }
        String[] top = {"0", "0", ""};
        list.add(0, top);
        return list; 
    }
       
    public List<String[]> getAvailableTimes(Integer mid) 
    {
        FieldE f = new FieldE();
        String[] slot = {"ERROR"};
        try {
            // XXXXXX BIG HACK !!!!
            String parentVar = getVarName(mid - 1);
            String fieldName = map.get(parentVar+"[]")[0];//_fieldr"+rid+"m"+(mid-1)+"[]")[0];
            // XXXXX
            f._name = fieldName;
            f = (FieldE)EntityE.retrieve(f).get(0);
            slot = f._times;
        }
        catch (Exception e) {
            Util.handleException(e);
        }
        List<String[]> list = new ArrayList<String[]>();
        Integer i = 1;
        for (String s : slot) {
            String[] triple = {i.toString(), s, s};
            list.add(triple);
            i++;
        }
        String[] other = {i.toString(), "Other", "Other"};
        list.add(other);
        String[] top = {"0", "0", "_time"};
        list.add(0, top);
        return list; 
    }
        
    public String getVarName(Integer mid) {
        String x = traitList.get(mid - 1).var;
        x += "r" + rid + "m" + mid;
        return x;
    }
    
}
